#通过查询数据库红人名称，通过红人名称判断，如果有则编辑，如果无则新增
#通过查询数据库红人ID，取红人ID最大值，按最大值新增红人数据
import requests
from common.setting import ensure_path_sep
from utils.mysqlUtils.db_pool import Database


def add_redskins(nick_name: str) -> int:
    redskins_db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "zhcxkj_test_Redskins_system")
    sql_row = f"SELECT * FROM t_celebrity WHERE nick_name = '{nick_name}'"
    redskins_row = redskins_db.execute(sql_row)
    redskins_list = redskins_db.fetch_all(f"SELECT * FROM t_celebrity WHERE nick_name ='{nick_name}'")
    redskins_max_id = redskins_db.fetch_max_id("SELECT MAX(id) FROM t_celebrity")
    redskins_max_id_add = redskins_max_id[0]['MAX(id)'] + 1

    if redskins_row == 0:

        url = "https://webapi.internal.zhcxkj.com/open/celebrity/list/add"
        method = "post"
        json = [
            {
                "avatarUrl": "https://zhcxkj-java-test.obs.cn-south-1.myhuaweicloud.com/celebrity/avatar/342e8331c66f454caa9ebf1d397d92f3.png",
                "avgCommissionRate": 0.12,
                "avgLiveGmv": 0,
                "avgLiveInteractionRate": 0.1011,
                "avgLiveNumberViewers": 438000,
                "avgVideoGmv": 0.145,
                "brandCooperation": 30,
                "buyerGmv": 32.98,
                "celebrityId": redskins_max_id_add,
                "celebrityLanguage": "en",
                "contactEmail": "1579211084@qq.com",
                "currency": "$",
                "dataTimeBegin": "2024-06-09",
                "dataTimeEnd": "2024-07-09",
                "ecAvgLiveInteractionRate": 1.2848,
                "ecAvgLiveNumberViewers": 4360,
                "ecLiveCount": 18154,
                "ecVideoCount": 4545,
                "ecVideoEngagement": 0.115,
                "ecVideoPlayCntMed": 5141,
                "expertLikes": 58450,
                "fans": 34519,
                "followerAgeList": [
                    {
                        "celebrityId": redskins_max_id_add,
                        "topFollowerAge": "18-24",
                        "topFollowerAgeZb": 6.14
                    },
                    {
                        "celebrityId": redskins_max_id_add,
                        "topFollowerAge": "25-34",
                        "topFollowerAgeZb": 3.64
                    },
                    {
                        "celebrityId": redskins_max_id_add,
                        "topFollowerAge": "35-44",
                        "topFollowerAgeZb": 6.4
                    },
                    {
                        "celebrityId": redskins_max_id_add,
                        "topFollowerAge": "45-54",
                        "topFollowerAgeZb": 5.4
                    },
                    {
                        "celebrityId": redskins_max_id_add,
                        "topFollowerAge": "55+",
                        "topFollowerAgeZb": 2.1
                    }
                ],
                "follows": 14619,
                "friends": 7722,
                "id": redskins_max_id_add,
                "likes": 6926,
                "liveCount": 929,
                "liveGmv": 0.265,
                "liveGpm": 0.154,
                "medGmvRevenue": 154,
                "nickName": nick_name,
                "platformId": 104,
                "ppKeywords": "奶冻回来",
                "ppVideoNumber": 1329,
                "ppVideoTime": 1720492969738,
                "ppVideoUrl": "https://zhcxkj-java-test.obs.cn-south-1.myhuaweicloud.com/celebrity/avatar/11c63ffdc8504567b8f91e0a1360c9a2.mp4",
                "productItem": 5107,
                "productPriceRange": "$10.00-$299.99",
                "region": "美国",
                "remark": "批量新增测试数据——夏宇测试",
                "sign": "夏宇测试——红人签名，桃子味",
                "tikHomepage": "https://webapi.internal.zhcxkj.com/doc.html#/zhcxkj-application-open/%E7%BA%A2%E4%BA%BA%E4%BF%A1%E6%81%AF%E8%A1%A8%E6%8E%A5%E5%8F%A3%E9%9B%86%E5%90%88/celebrityListAddUsingPOST",
                "topFollowerAge": "18-24",
                "topFollowerGender": 20,
                "topFollowerGenderZb": 0.69,
                "totalTransactionVolume": 1561,
                "transactionAmount": 1535,
                "uid": "710754128925204585455",
                "uniqueId": "漆老大——1",
                "unitsSold": 55,
                "unitsSoldRange": "产品销售范围+动漫",
                "upStatus": 0,
                "upTime": 1720492969739,
                "videoAvgViewCnt": 816,
                "videoCount": 354,
                "videoEngagement": 0.574,
                "videoGmv": 4524,
                "videoGpm": 44,
                "videoMedViewCnt": 4.51,
                "videos": 8179
            }
        ]

        headers = {
            "Accept": "application/json",
            "Authorization_V2": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21udW1iZXIiOiI2Yjg1OTE1OS1kZWRkLTRhMGMtYWM3Yi05M2E1ZWVlMjEyODYiLCJ1c2VyX2lkIjoxNjE4NiwidXNlcl9rZXkiOiJlYTdkYjgyYjM5M2UzNjBiMjAzNzlhYWIzYTUxZTQ5MCIsInVzZXJuYW1lIjoi5aSP5a6HIn0.XaWC-P6x_Q0UWpuSvaNAx5vzlzhXN5uAfBjhZaX4I2p2psXN88MqoNEOFsgouPvLuAm8veLbFga6BLw4fDSwZA",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate, br",
            "Content-Type": "application/json;charset=UTF-8",
            "X-Requested-With": "XMLHttpRequest",
            "Connection": "keep-alive",
            "TE": "trailers",
            "Authorization": "Basic emhjeGphdmE6MTIzNDU2",
        }

        response = requests.request(method=method, url=url, json=json, headers=headers)
        print(response.json())
        return redskins_max_id_add
    else:

        url = "https://webapi.internal.zhcxkj.com/open/celebrity/list/update"
        method = "post"
        json = [
            {
                "avatarUrl": "https://zhcxkj-java-test.obs.cn-south-1.myhuaweicloud.com/celebrity/avatar/342e8331c66f454caa9ebf1d397d92f3.png",
                "avgCommissionRate": 0.12,
                "avgLiveGmv": 0,
                "avgLiveInteractionRate": 0.1011,
                "avgLiveNumberViewers": 438000,
                "avgVideoGmv": 0.145,
                "brandCooperation": 30,
                "buyerGmv": 32.98,
                "celebrityId": redskins_max_id,
                "celebrityLanguage": "en",
                "contactEmail": "1579211084@qq.com",
                "currency": "$",
                "dataTimeBegin": "2024-06-09",
                "dataTimeEnd": "2024-07-09",
                "ecAvgLiveInteractionRate": 1.2848,
                "ecAvgLiveNumberViewers": 4360,
                "ecLiveCount": 18154,
                "ecVideoCount": 4545,
                "ecVideoEngagement": 0.115,
                "ecVideoPlayCntMed": 5141,
                "expertLikes": 58450,
                "fans": 34519,
                "followerAgeList": [
                    {
                        "celebrityId": redskins_max_id,
                        "topFollowerAge": "18-24",
                        "topFollowerAgeZb": 6.14
                    },
                    {
                        "celebrityId": redskins_max_id,
                        "topFollowerAge": "25-34",
                        "topFollowerAgeZb": 3.64
                    },
                    {
                        "celebrityId": redskins_max_id,
                        "topFollowerAge": "35-44",
                        "topFollowerAgeZb": 6.4
                    },
                    {
                        "celebrityId": redskins_max_id,
                        "topFollowerAge": "45-54",
                        "topFollowerAgeZb": 5.4
                    },
                    {
                        "celebrityId": redskins_max_id,
                        "topFollowerAge": "55+",
                        "topFollowerAgeZb": 2.1
                    }
                ],
                "follows": 14619,
                "friends": 7722,
                "id": redskins_max_id,
                "likes": 6926,
                "liveCount": 929,
                "liveGmv": 0.265,
                "liveGpm": 0.154,
                "medGmvRevenue": 154,
                "nickName": nick_name,
                "platformId": 104,
                "ppKeywords": "奶冻回来",
                "ppVideoNumber": 1329,
                "ppVideoTime": 1720492969738,
                "ppVideoUrl": "https://zhcxkj-java-test.obs.cn-south-1.myhuaweicloud.com/celebrity/avatar/11c63ffdc8504567b8f91e0a1360c9a2.mp4",
                "productItem": 5107,
                "productPriceRange": "$10.00-$299.99",
                "region": "美国",
                "remark": "批量新增测试数据——夏宇测试",
                "sign": "夏宇测试——红人签名，桃子味",
                "tikHomepage": "https://webapi.internal.zhcxkj.com/doc.html#/zhcxkj-application-open/%E7%BA%A2%E4%BA%BA%E4%BF%A1%E6%81%AF%E8%A1%A8%E6%8E%A5%E5%8F%A3%E9%9B%86%E5%90%88/celebrityListAddUsingPOST",
                "topFollowerAge": "18-24",
                "topFollowerGender": 20,
                "topFollowerGenderZb": 0.69,
                "totalTransactionVolume": 1561,
                "transactionAmount": 1535,
                "uid": "710754128925204585455",
                "uniqueId": "漆老大——1",
                "unitsSold": 55,
                "unitsSoldRange": "产品销售范围+动漫",
                "upStatus": 0,
                "upTime": 1720492969739,
                "videoAvgViewCnt": 816,
                "videoCount": 354,
                "videoEngagement": 0.574,
                "videoGmv": 4524,
                "videoGpm": 44,
                "videoMedViewCnt": 4.51,
                "videos": 8179
            }
        ]

        headers = {
            "Accept": "application/json",
            "Authorization_V2": "Bearer eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21udW1iZXIiOiI2Yjg1OTE1OS1kZWRkLTRhMGMtYWM3Yi05M2E1ZWVlMjEyODYiLCJ1c2VyX2lkIjoxNjE4NiwidXNlcl9rZXkiOiJlYTdkYjgyYjM5M2UzNjBiMjAzNzlhYWIzYTUxZTQ5MCIsInVzZXJuYW1lIjoi5aSP5a6HIn0.XaWC-P6x_Q0UWpuSvaNAx5vzlzhXN5uAfBjhZaX4I2p2psXN88MqoNEOFsgouPvLuAm8veLbFga6BLw4fDSwZA",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate, br",
            "Content-Type": "application/json;charset=UTF-8",
            "X-Requested-With": "XMLHttpRequest",
            "Connection": "keep-alive",
            "TE": "trailers",
            "Authorization": "Basic emhjeGphdmE6MTIzNDU2",
        }

        response = requests.request(method=method, url=url, json=json, headers=headers)
        print(response.json())
        return redskins_list[0]["id"]


if __name__ == '__main__':

    redskins_id = add_redskins("漆老大")
    print(redskins_id)